﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using Twitterizer;
using TwiMixer.RESTAPI;
using System.Configuration;

namespace TwiMixer.Utility
{
    public static class UserConfig
    {
        public static void SaveUserToken(OAuthTokenResponse response, string apiProviderName)
        {
            string xmlFile = @"d:\TwiMixerWorkspace\user\";
            xmlFile += apiProviderName + @"\" + response.UserId + ".tm";

            XmlTextWriter writer = new XmlTextWriter(xmlFile, null);

            writer.Formatting = Formatting.Indented;
            writer.Indentation = 3;
            writer.WriteStartDocument();

            writer.WriteStartElement("TwiMixer");

            //writer.WriteStartElement("User");
            //writer.WriteAttributeString("name", "larvata");
            //writer.WriteAttributeString("password", "abcdefg");
            //writer.WriteAttributeString("uid", "1500130");
            //writer.WriteEndElement();

            writer.WriteStartElement("RESTAPI");
            writer.WriteStartElement("API");
            writer.WriteAttributeString("name", apiProviderName);
            writer.WriteAttributeString("enabled", "true");
            writer.WriteElementString("token", response.Token);
            writer.WriteElementString("secret", response.TokenSecret);

            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }

        public static OAuthTokens LoadUserToken(string path)
        {
            string[] requestPath = path.Split('/');
            OAuthTokens token = new OAuthTokens();

            if (requestPath.Length != 6)
            {
                //返回地址无效页面 未完成
                string xmlFile = @"d:\TwiMixerWorkspace\user\";
                xmlFile += requestPath[2] + @"\" + requestPath[3] + ".tm";

                XmlTextReader reader = new XmlTextReader(xmlFile);
                reader.ReadStartElement("TwiMixer");
                reader.ReadStartElement("RESTAPI");
                while (reader.Read())
                {
                    //确定api类型是否相符 sina=s twitter=t
                    //为以后个人配置保存在单个文件做准备 现在用不到
                    if (reader.GetAttribute("name") == requestPath[2] && reader.NodeType == XmlNodeType.Element)
                    {
                        reader.ReadStartElement("API");
                        token.AccessToken = reader.ReadElementString("token");
                        token.AccessTokenSecret = reader.ReadElementString("secret");
                    }
                }
                reader.Close();
            }
            return token;
        }

        public static void LoadProviderConfig(ref OAuthTokens token, APIProviderEnum api)
        {
            switch (api)
            {
                case APIProviderEnum.twimixer:
                    break;
                case APIProviderEnum.tweet:
                    token.ConsumerKey = ConfigurationManager.AppSettings["Twitter_Consumer_Key"];
                    token.ConsumerSecret = ConfigurationManager.AppSettings["Twitter_Consumer_Secret"];
                    break;
                case APIProviderEnum.sina:
                    token.ConsumerKey = ConfigurationManager.AppSettings["Sina_App_Key"];
                    token.ConsumerSecret = ConfigurationManager.AppSettings["Sina_App_Secret"];
                    break;
                case APIProviderEnum.tencent:
                    break;
                default:
                    break;
            }

        }
    }
}